<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- crossorigin="anonymous" 即向源发送请求时不携带cookie -->
    <link
      crossorigin="anonymous"
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.css"
      rel="stylesheet"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js"></script>
    <!-- <script
      crossorigin="anonymous"
      src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"
    ></script> -->
  </head>
  <body>
    <div class="container">
      <h2>fetch发送Ajax请求</h2>
      <button class="btn btn-info">fetch发送ajax请求</button>
    </div>
  </body>

  <script>
    $("button")
      .eq(0)
      .click(function () {
        fetch("http://127.0.0.1:9000/server2", {
          //方法
          method: "post",
          //请求头
          headers: {},
          //请求体
          body: "username=amdin&pwd=123",
        })
          .then((res) => {
            //return res.text();
            return res.json(); //如果res是json 可以直接使用res.json()
          })
          .then((res) => {
            console.log(res); //输出上一个then return的数据
          });
      });
  </script>
</html>
